package com.lzx.iteam.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.baidu.mapapi.map.Text;
import com.lzx.iteam.*;
import com.lzx.iteam.bean.*;
import com.lzx.iteam.util.AllDialogUtil;
import com.lzx.iteam.util.DateUtil;
import com.lzx.iteam.util.ImageLoaderInterface;
import com.lzx.iteam.util.StringUtil;
//import jp.sourceforge.qrcode.geom.Line;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Megan on 16/4/8.
 */
public class AttendanceCalendarListAdapter extends BaseAdapter {

    private Context mContext;
    private List<Object> mListData = new ArrayList<Object>();
    private boolean isMy;         // true：是我本人      false：不是本人
    private String userId;
    private int mFormType;
    private String mSubName;

    public AttendanceCalendarListAdapter(Context mContext, boolean isMy, String userId) {
        this.mContext = mContext;
        this.isMy = isMy;
        this.userId = userId;
    }

    public void setSubName(int type,String name){
        this.mFormType = type;
        this.mSubName = name;
    }

    public void bindData(List<Object> list) {
        this.mListData = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mListData.size();
    }

    @Override
    public Object getItem(int position) {
        return mListData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return mListData.get(position) instanceof AttendanceCalendarMsgData ? 0 : 1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        LegworkHolder legworkHolder = null;
        if (getItemViewType(position) == 0) {
            if (convertView != null && convertView.getTag().getClass().equals(ViewHolder.class)) {
                holder = (ViewHolder) convertView.getTag();
            } else {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.attendance_calendar_list_item_layout, null);
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.tv_attendance_calendar_list_item_text);
                holder.time = (TextView) convertView.findViewById(R.id.tv_attendance_calendar_list_item_time);
                holder.msg = (TextView) convertView.findViewById(R.id.tv_attendance_calendar_list_item_msg);
                holder.note = (TextView) convertView.findViewById(R.id.tv_attendance_calendar_list_item_q);
                holder.approval = (TextView) convertView.findViewById(R.id.tv_attendance_calendar_list_item_approval);
                holder.arrow = (ImageView) convertView.findViewById(R.id.iv_attendance_calendar_list_item_arrow);
                holder.approvalLayout = (LinearLayout) convertView.findViewById(R.id.ll_attendance_calendar_list_item_approval);
                holder.topLayout = (RelativeLayout) convertView.findViewById(R.id.rl_attendance_calendar_list_item_top);
                holder.topText = (TextView) convertView.findViewById(R.id.tv_attendance_calendar_list_item_top_text);
                holder.tvFix = (TextView) convertView.findViewById(R.id.tv_at_item_fix);
                convertView.setTag(holder);
            }
        } else {
            if (convertView != null && convertView.getTag().getClass().equals(LegworkHolder.class)) {
                legworkHolder = (LegworkHolder) convertView.getTag();
            } else {
                legworkHolder = new LegworkHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.legwork_sign_calendar_item, null);
                legworkHolder.legworkTime = (TextView) convertView.findViewById(R.id.legwork_calendar_item_tv_time);
                legworkHolder.legworkAddress = (TextView) convertView.findViewById(R.id.legwork_calendar_item_tv_address);
                legworkHolder.legworkImage = (ImageView) convertView.findViewById(R.id.legwork_calendar_item_iv);
                legworkHolder.legworkTop = (LinearLayout) convertView.findViewById(R.id.legwork_calendar_item_top);
                convertView.setTag(legworkHolder);
            }
        }

        if (mListData != null && mListData.size() > 0) {
            if (mListData.get(position) instanceof AttendanceCalendarMsgData) {
                final AttendanceCalendarMsgData attendanceCalendarMsgData = (AttendanceCalendarMsgData) mListData.get(position);
                if (mFormType == 0){
                    if (position == 0) {
                        holder.topLayout.setVisibility(View.VISIBLE);
                        holder.tvFix.setVisibility(View.VISIBLE);
                        if ("1".equals(attendanceCalendarMsgData.getPart()) || "3".equals(attendanceCalendarMsgData.getPart())) {
                            holder.topText.setText("上午签到");
                        } else if ("2".equals(attendanceCalendarMsgData.getPart()) || "4".equals(attendanceCalendarMsgData.getPart())) {
                            holder.topText.setText("下午签到");
                        }

                    } else {
                        holder.tvFix.setVisibility(View.GONE);
                        if ("1".equals(((AttendanceCalendarMsgData) mListData.get(position - 1)).getPart())) {
                            if("4".equals(((AttendanceCalendarMsgData) mListData.get(position)).getPart())||"2".equals(((AttendanceCalendarMsgData) mListData.get(position)).getPart())){
                                holder.topLayout.setVisibility(View.VISIBLE);
                                holder.topText.setText("下午签到");
                            }else{
                                holder.topLayout.setVisibility(View.GONE);
                            }
                        } else if ("4".equals(((AttendanceCalendarMsgData) mListData.get(position - 1)).getPart())) {
                            holder.topLayout.setVisibility(View.GONE);
                        } else {
                            holder.topLayout.setVisibility(View.VISIBLE);
                            if ("1".equals(attendanceCalendarMsgData.getPart()) || "3".equals(attendanceCalendarMsgData.getPart())) {
                                holder.topText.setText("上午签到");
                            } else if ("2".equals(attendanceCalendarMsgData.getPart()) || "4".equals(attendanceCalendarMsgData.getPart())) {
                                holder.topText.setText("下午签到");
                            }
                        }
                    }
                }else{
                    if (position == 0){
                        holder.topLayout.setVisibility(View.VISIBLE);
                        holder.tvFix.setVisibility(View.VISIBLE);
                        if (!StringUtil.isEmpty(mSubName))
                        holder.topText.setText("班次：" + mSubName);
                    }else{
                        holder.topLayout.setVisibility(View.GONE);
                        holder.tvFix.setVisibility(View.GONE);
                    }
                }
                if ("1".equals(attendanceCalendarMsgData.getType())) {
                    holder.arrow.setVisibility(View.INVISIBLE);
                    if (attendanceCalendarMsgData.getTime() != null)
                        holder.time.setText(DateUtil.getHourAndMin(Long.parseLong(attendanceCalendarMsgData.getTime()) * 1000));
                    holder.approvalLayout.setVisibility(View.VISIBLE);
                    holder.approval.setText("申诉");
                    holder.approval.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
                    if ("0".equals(attendanceCalendarMsgData.getStatus())) {
                        holder.msg.setText("未打卡");
                        holder.time.setText("无");
                        holder.msg.setTextColor(mContext.getResources().getColor(R.color.attendance_calendar_red));
                    } else if ("1".equals(attendanceCalendarMsgData.getStatus())) {

                        holder.msg.setText("迟到");
                        holder.msg.setTextColor(mContext.getResources().getColor(R.color.attendance_calendar_red));
                    } else if ("2".equals(attendanceCalendarMsgData.getStatus())) {

                        holder.msg.setText("早退");
                        holder.msg.setTextColor(mContext.getResources().getColor(R.color.attendance_calendar_red));
                    } else if ("6".equals(attendanceCalendarMsgData.getStatus())) {
                        holder.approvalLayout.setVisibility(View.INVISIBLE);
                        holder.msg.setText("打卡");
                        holder.msg.setTextColor(mContext.getResources().getColor(R.color.schedule_notbusy_text));
                    } else if ("7".equals(attendanceCalendarMsgData.getStatus())) {
                        holder.approvalLayout.setVisibility(View.INVISIBLE);
                        holder.msg.setText("打卡");
                        holder.msg.setTextColor(mContext.getResources().getColor(R.color.schedule_notbusy_text));
                    }
                } else if ("2".equals(attendanceCalendarMsgData.getType())) {
                    holder.approvalLayout.setVisibility(View.INVISIBLE);
                    holder.arrow.setVisibility(View.VISIBLE);
                    holder.time.setText("无");
                    if ("1".equals(attendanceCalendarMsgData.getApproval_type())) {
                        holder.msg.setText("出差");
                        holder.msg.setTextColor(mContext.getResources().getColor(R.color.schedule_notbusy_text));
                    } else if ("2".equals(attendanceCalendarMsgData.getApproval_type())) {
                        holder.msg.setText("请假");
                        holder.msg.setTextColor(mContext.getResources().getColor(R.color.schedule_notbusy_text));
                    } else if ("3".equals(attendanceCalendarMsgData.getApproval_type())) {
                        holder.msg.setText("外出");
                        holder.msg.setTextColor(mContext.getResources().getColor(R.color.schedule_notbusy_text));
                    } else if ("4".equals(attendanceCalendarMsgData.getApproval_type())) {
                        if (isMy) {
                            holder.msg.setText("");
                        } else {
                            holder.msg.setText("已申诉");
                        }
                        if ("1".equals(attendanceCalendarMsgData.getPart()) || "3".equals(attendanceCalendarMsgData.getPart())) {
                            holder.time.setText("上午");
                        } else if ("2".equals(attendanceCalendarMsgData.getPart()) || "4".equals(attendanceCalendarMsgData.getPart())) {
                            holder.time.setText("下午");
                        }

                        holder.msg.setTextColor(mContext.getResources().getColor(R.color.schedule_notbusy_text));
                        holder.approvalLayout.setVisibility(View.VISIBLE);
                        holder.approval.setText("已申诉");
                        holder.approval.setTextColor(mContext.getResources().getColor(R.color.schedule_notbusy_text));

                    }
                }
                if ("1".equals(attendanceCalendarMsgData.getPart())) {
                    if (mFormType == 0){
                        holder.text.setText("签到时间:");
                    }else{
                        holder.text.setText("上班:");
                    }
                } else if ("2".equals(attendanceCalendarMsgData.getPart())) {
                    if (mFormType == 0){
                        holder.text.setText("签退时间:");
                    }else{
                        holder.text.setText("下班:");
                    }
                } else if ("3".equals(attendanceCalendarMsgData.getPart())) {
                    if (mFormType == 0){
                        holder.text.setText("签退时间:");
                    }else{
                        holder.text.setText("下班:");
                    }
                } else if ("4".equals(attendanceCalendarMsgData.getPart())) {
                    if (mFormType == 0){
                        holder.text.setText("签到时间:");
                    }else{
                        holder.text.setText("上班:");
                    }
                }
                if ("1".equals(attendanceCalendarMsgData.getCheat())) {
                    holder.note.setVisibility(View.VISIBLE);
                } else {
                    holder.note.setVisibility(View.GONE);
                }

                if (!isMy) {
                    holder.approvalLayout.setVisibility(View.INVISIBLE);
                }

                holder.note.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //  showCheatNote();
                        Intent intent = new Intent(mContext, AttendanceDeviceErrorActivity.class);
                        intent.putExtra("groupId", attendanceCalendarMsgData.getGroup_id());
                        intent.putExtra("deviceId", attendanceCalendarMsgData.getCheat_device_id());
                        intent.putExtra("setUid", userId);
                        mContext.startActivity(intent);
                    }
                });

                holder.tvFix.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, AttendanceFixDataActivity.class);
                        intent.putExtra("attendance_id",attendanceCalendarMsgData.getAtten_calendar_id());
                        mContext.startActivity(intent);
                    }
                });


                holder.approvalLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!"4".equals(attendanceCalendarMsgData.getApproval_type())) {
                    /*Intent intent = new Intent(mContext, ApprovalCreateActivity.class);
                    ApprovalData approvalData = new ApprovalData();
                    approvalData.setApproval_start_time(attendanceCalendarMsgData.getTime());
                    approvalData.setApproval_start_part(Integer.parseInt(attendanceCalendarMsgData.getPart()));
                    approvalData.setApproval_type(4);
                    approvalData.setApproval_group_id(attendanceCalendarMsgData.getGroup_id());
                    intent.putExtra("approvalData", approvalData);
                    mContext.startActivity(intent);*/
                            ((AttendanceCalendarActivity) mContext).sendApproval(attendanceCalendarMsgData);
                        }
                    }
                });

            } else if (mListData.get(position) instanceof LegworkRecordData) {

                if (position == 0) {
                    legworkHolder.legworkTop.setVisibility(View.VISIBLE);
                } else if (mListData.get(position - 1) instanceof AttendanceCalendarMsgData) {
                    legworkHolder.legworkTop.setVisibility(View.VISIBLE);
                } else {
                    legworkHolder.legworkTop.setVisibility(View.GONE);
                }
                final LegworkRecordData legworkRecordData = (LegworkRecordData) mListData.get(position);
                legworkHolder.legworkTime.setText("签到时间:" + DateUtil.getHourAndMin(Long.parseLong(legworkRecordData.getTime()) * 1000));
                legworkHolder.legworkAddress.setText(legworkRecordData.getAddress());

                if (!StringUtil.isEmpty(legworkRecordData.getAttachment())) {
                    legworkHolder.legworkImage.setVisibility(View.VISIBLE);
                    try {
                        JSONArray array = new JSONArray(legworkRecordData.getAttachment());
                        if (array.length() > 0) {
                            JSONObject attachment = array.getJSONObject(0);
                            String image = attachment.getString("thumb");
                            ImageLoaderInterface.imageLoader.displayImage(image, legworkHolder.legworkImage, ImageLoaderInterface.optionEventImage);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    legworkHolder.legworkImage.setVisibility(View.GONE);
                }


            }
        }


        return convertView;
    }

    class ViewHolder {
        TextView text, time, note, msg, approval, topText;
        LinearLayout approvalLayout;
        RelativeLayout topLayout;
        TextView tvFix;
        ImageView arrow;
    }

    class LegworkHolder {
        LinearLayout legworkTop;
        TextView legworkTime, legworkAddress;
        ImageView legworkImage;
    }


/*
    private void showCheatNote() {
        AllDialogUtil allDialogUtil = AllDialogUtil.getInstance((AttendanceCalendarActivity) mContext);
        allDialogUtil.titleMsgBtnStyle("提示", "本次考勤操作非绑定设备上操作", mContext.getString(R.string.ok));
        allDialogUtil.setOnDialogListener(new AllDialogUtil.DialogOnClickListener() {

            @Override
            public void onConfirmClick(Dialog lDialog) {
                lDialog.dismiss();
            }

            @Override
            public void onCancelClick(Dialog lDialog) {
            }
        });
    }
*/
}